﻿using System.Collections.Generic;
using NetBay.Core.BusinessEntities.Workflow;
using Rio.Flow.Business.Repository;
using Rio.Workflow.Runtime;
using System.Linq;

namespace Rio.Workflow.Information
{
    public class Information : WorkflowContext
    {
        #region Properties

        /// <summary>
        /// Gets the type of the WKF.
        /// </summary>
        /// <value>The type of the WKF.</value>
        public override WorkflowType WkfType
        {
            get { return WorkflowType.Information; }
        }

        #endregion

        #region Constructors
        #endregion

        #region Public functions

        /// <summary>
        /// Gets the new state of the workflow.
        /// </summary>
        /// <returns></returns>
        public override WorkflowState GetNewWorkflowState()
        {
            if (this.InstanceIsFinished())
            {
                return WorkflowState.Read;
            }
            return WorkflowState.None;
        }

        /// <summary>
        /// In information workflow, the instance needs to be set to finished when all users have read the instance
        /// </summary>
        /// <returns>Indicates whether if the instance must to be close.</returns>
        public override bool InstanceIsFinished()
        {
            List<WorkflowInstanceUser> instanceUsers = WorkflowInstanceRepository.GetUserInstances(this.CurrentInstance.Identifier);

            //If all users have read => finish
            return instanceUsers.All(instanceUser => instanceUser.IsRead);
        }

        #endregion
    }
}
